<template>
	<view class="subscribe_pay_layout">
		<view class="pay-body">
			<web-view :fullscreen="true" :webview-styles="{width:'100%', height:'100%'}"
				:style="{width:'100%', height:'100%'}" ref="webview" id="webview" :src="payUrl" @message="handleMessage"
				@load="onWebviewLoad">
			</web-view>
		</view>
	</view>
</template>

<script setup>
	import {
		onLoad,
		onReady,
		onBackPress
	} from "@dcloudio/uni-app";
	import {
		ref
	} from "vue";
	import {
		apiGetDevicePosition,
	} from "../../api/device_api";
	var payUrl = ref(null);
	var imeiData = ref(null)
	var wv; //计划创建的webview
	var canBack = false;
	var resData = ref(null)
	onLoad(async (e) => {
		
		let {
			url = null,
			imei = null
		} = e;
		payUrl.value = url;
		imeiData.value = imei;
		
		getDeviceInfo();
	})
	
	async function getDeviceInfo() {
		var param = {
			imei:imeiData.value
		}
		let res = await apiGetDevicePosition(param);
		resData.value = res.content;
	}
	
	const handleMessage = (e) => {
		
		//订阅中或订阅到期 重新订阅的  无需设置名称 
		if(resData.value.subscribed == 1|| resData.value.liveMode == 4){
			let pages = getCurrentPages()
			let page = pages[pages.length - 1];
			let currentPages = page.$getAppWebview()
			let currentPage = currentPages.children()[0];
			currentPage.clear(); 
			uni.$emit("stepTwo", true);
			uni.navigateBack({
				delta:2
			});
		}else{
			uni.$emit("stepTwo", true);
			uni.navigateTo({
				url: "/pages/add_device/name-device?data=" + imeiData.value
			})
		}  
	}
	
</script>

<style lang="scss" scoped>
	.subscribe_pay_layout {
		.pay-body {
			width: 100%;
			height: 100%;
		}
	}
</style>